import uvicorn
from fastapi import FastAPI
import openai
openai.api_base = 'yourapibase'
openai.api_key = 'yourapikey'



























app = FastAPI()
info = {"name":"","age":"","region":"","filetype":"","hoobit":""}
@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/name/{name}")
def read_item(name: str):
    info["name"] = name
    return "ok"

@app.get("/age/{age}")
def read_item(age: str):
    info["age"] = age
    return "ok"

@app.get("/region/{region}")
def read_item(region: str):
    info["region"] = region
    return "ok"

@app.get("/filetype/{filetype}")
def read_item(filetype: str):
    info["filetype"] = filetype
    return "ok"

@app.get("/hoobit/{hoobit}")
def read_item(hoobit: str):
    info["hoobit"] = hoobit
    return "ok"




@app.get("/res")
def read_item():
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a chatbot"},
            {"role": "user",
             "content":
                 "我有一个朋友，他的名字叫" + str(info["name"]) + ",他现在" + str(info["age"]) + "岁，他是" + str(
                     info["region"]) + "人，他的爱好是" + str(
                     info["hoobit"]) + "请写一封信给他，信中要求要有附件，附件的格式为" + str(
                     info["filetype"])+",并将内容翻译为英文"},
        ]
    )

    res = response.choices[0].message.get("content", "")
    return res

if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)